package cz.matasek.dbLogger.gui;

import cz.matasek.dbLogger.Constant;
import cz.matasek.dbLogger.Settings;
import java.awt.Insets;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
 * The class contains all dialogs used throughout the application.
 *
 * @author Luboš Matásek
 */
public class Dialog {

    /**
     * The class is not instantiable.
     */
    private Dialog() {}

    /**
     * Shows an error dialog when a database error occurrs.
     *
     * @param errorMessage error message
     */
    public static void error(String errorMessage) {
        JOptionPane.showMessageDialog(
                Gui.getInstance(),
                "An error has occurred:\n" + errorMessage,
                "Error",
                JOptionPane.ERROR_MESSAGE);
    }

    /**
     * Shows a dialog with the SQL query.
     * 
     * @param sqlQuery the SQL query
     */
    public static void SQLQuery(String sqlQuery) {
        JTextArea sqlQueryArea = new JTextArea(sqlQuery);
        sqlQueryArea.setEditable(false);
        sqlQueryArea.setMargin(new Insets(10, 10, 10, 10));

        JDialog sqlQueryDialog = 
                new JDialog(Gui.getInstance(), "SQL query", true);
        sqlQueryDialog.setResizable(false);
        sqlQueryDialog.add(sqlQueryArea);
        sqlQueryDialog.pack();
        sqlQueryDialog.setVisible(true);
    }

    /**
     * Shows an input dialog to specify the logging prefix.
     * 
     * @return the logging prefix
     */
    public static String loggingPrefix() {
        return (String) JOptionPane.showInputDialog(
                Gui.getInstance(),
                "Please specify the logging prefix.",
                "Logging prefix",
                JOptionPane.QUESTION_MESSAGE,
                null,
                null,
                Settings.get(Settings.Key.DEFAULT_LOGGING_PREFIX));
    }

    /**
     * Shows the dialog to confirm dropping the logging environment.
     *
     * @return <code>true</code> if dropping is confirmed; <code>false</code>
     * otherwise
     */
    public static boolean dropLoggingEnvironment() {
        int answer = JOptionPane.showConfirmDialog(
                Gui.getInstance(),
                "Are you sure you wish to drop the logging environment?",
                "Drop logging environment?",
                JOptionPane.OK_CANCEL_OPTION);

        if (answer == JOptionPane.OK_OPTION) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Shows the dialog to confirm dropping the logging table.
     *
     * @return <code>true</code> if dropping is confirmed; <code>false</code>
     * otherwise
     */
    public static boolean dropLoggingTable() {
        int answer = JOptionPane.showConfirmDialog(
                Gui.getInstance(),
                "Are you sure you wish to drop the logging table?",
                "Drop logging table?",
                JOptionPane.OK_CANCEL_OPTION);

        if (answer == JOptionPane.OK_OPTION) {
            return true;
        } else {
            return false;
        }
    }
}
